package cn.rdq.iknesssystem.service.impl;

import cn.rdq.api.po.Views;
import cn.rdq.dao.ViewsMapper;
import cn.rdq.iknesssystem.service.ViewsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ViewsServiceImpl extends ServiceImpl<ViewsMapper, Views> implements ViewsService {
    @Override
    public List<Views> getRecentViews(int days) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_YEAR, -30);
        int year = instance.get(Calendar.YEAR);
        //月份范围 [0,11] 因此+1
        int month = instance.get(Calendar.MONTH) + 1;
        int day = instance.get(Calendar.DAY_OF_MONTH);
        String today = year + "-" + month + "-" + day;
        QueryWrapper<Views> wrapper = new QueryWrapper<>();
        wrapper.ge("date_history", today);
        wrapper.orderByAsc("date_history");
        List<Views> views = list(wrapper);
        for (Views view : views) {
            view.setDate(view.getDateHistory());
        }
        return views;
    }
}
